package com.dk.service.impl;

import com.dk.mapper.RoleMapper;
import com.dk.mapper.UserMapper;
import com.dk.mapper.UserRoleMapper;
import com.dk.pojo.Role;
import com.dk.pojo.User;
import com.dk.pojo.UserRoleExample;
import com.dk.service.UserService;
import com.dk.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<User> selectInfo() {
        List<User> users = userMapper.selectByExample(null);
        return users;
    }

    @Override
    public List<Role> selctBRole() {
        List<Role> roles = roleMapper.selectByExample(null);
        return roles;
    }

    @Override
    public boolean insert(User user, Integer[] rid) {
        try {


            user.setPassword("123");
            Integer a = userMapper.insertinto(user);
            Integer r = userRoleMapper.insertinfo(rid, user.getUid());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public User selctBuid(Integer uid) {
        User user = userMapper.selectByPrimaryKey(uid);
        return user;
    }

    @Override
    public List<Role> selecBrole() {
        List<Role> roles = roleMapper.selectByExample(null);
        return roles;
    }

    @Override
    public List<Integer> selectRid(Integer uid) {
        List<Integer> a = userRoleMapper.selectByUid(uid);
        return a;
    }

    @Override
    public boolean updateInByRid(User u, Integer[] rid) {
        try {


            int i = userMapper.updateByPrimaryKeySelective(u);
            UserRoleExample a = new UserRoleExample();
            UserRoleExample.Criteria criteria = a.createCriteria();
            UserRoleExample.Criteria criteria1 = criteria.andUidEqualTo(u.getUid());
            userRoleMapper.deleteByExample(a);
            Integer insertinfo = userRoleMapper.insertinfo(rid, u.getUid());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

    }

    @Override
    public boolean deleteByid(Integer id) {
        try {


            userMapper.deleteByPrimaryKey(id);
            UserRoleExample example = new UserRoleExample();
            UserRoleExample.Criteria criteria = example.createCriteria();
            criteria.andUidEqualTo(id);
            int i = userRoleMapper.deleteByExample(example);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    //登录
    @Override
    public Map<String, Object> selectBynamepassword(String uname, String password) {
        String failKey = "user:login:count:fail:" + uname;
        String locKey = " user:login:time:lock:" + uname;
        //        登陆的错误次数key :
//         锁定限制登录key
        Map map = new HashMap();
        map.put("chil", false);
        if (uname == null || uname.equals("") || password == null || password.equals("")) {
            map.put("message", "账号密码不能为了空");
            return map;
        }
        if (redisTemplate.hasKey(locKey)) {
            map.put("message", "账号被冻结 ,请在" + redisTemplate.getExpire(locKey, TimeUnit.MINUTES) + "在试");
            return map;
        }
        User u = userMapper.selectByname(uname);
        if (u == null) {
            map.put("message", "账号不存爱");
            return map;
        }
        try {
            boolean b = MD5Utils.validPassword(password, u.getPassword());
            if (!b) {
                if (!redisTemplate.hasKey(failKey)) {
                    redisTemplate.opsForValue().set(failKey, "1");
                    redisTemplate.expire(failKey, 2, TimeUnit.MINUTES);
                    map.put("message", "在两分钟里还可以输入密码4次");
                    return map;
                } else {
                    Long count = Long.parseLong(redisTemplate.opsForValue().get(failKey).toString());

                    if (count < 4) {
                        redisTemplate.opsForValue().increment(failKey, 1);
                        Long expire = redisTemplate.getExpire(failKey, TimeUnit.SECONDS);
                        map.put("message", "在" + expire + "秒,还可以输入密码次数" + (4 - count));
                        return map;
                    } else {
                        redisTemplate.opsForValue().set(locKey, "lock");
                        redisTemplate.expire(locKey, 1, TimeUnit.HOURS);
                        map.put("message", "在一个小时里不可以登录");
                        return map;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        redisTemplate.delete(failKey);
        map.put("chil", true);
        return map;

    }

    @Override
    public boolean selectBname(String uname) {
        User user = userMapper.selectByname(uname);
        if (user == null) {
            return true;
        }
        return false;
    }

    @Override
    public boolean insertInfo(User y) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String encryptedPwd = "";
        encryptedPwd = MD5Utils.getEncryptedPwd(y.getPassword());
        y.setPassword(encryptedPwd);
        int i = userMapper.insertSelective(y);
        if (i > 0) {
            return true;
        }
        return false;
    }
}